import storage from '@/utils/storage'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'
import {
	isJson,
	isNull
} from '@/utils/validate.js'


const constant = {
	avatar: 'avatar',
	name: 'name',
	roles: 'roles',
	permissions: 'permissions',
	user: 'user',
	menu: 'menu',
	perms: 'perms',
	token: getToken(),
	indexTopBar: 'indexTopBar',
	getState: function() {
		let asdfasd = {}
		for (let key in constant) {

			let asdf = asdfasd[key] = storage.get(constant[key])
			if (isNull(asdf)) {
				asdfasd[key] = asdf
			}

			if (key == 'token') {
				asdfasd[key] = getToken()
			}

		}

		return asdfasd
	},
	/**
	 * 初始化
	 */
	initIndexTopBar() {

		return new Promise((resolve, reject) => {
			let asdfasdfasd = []
			let menu = storage.get(constant['menu'])
			let combinedArray = [].concat(...menu.map(obj => obj.children));
			getOrSetJsonFile({
				filename: "IndexTopBar"
			}).then(res => {
				//如果云端有 就用云上的
				if (isNull(res.data) || !isJson(res.data)) {

					let list = [
						'测评中心',
						'我的客户',
						'会员信息',
						'会员合同',
						"报名续费记录",
						"我的学生",
						"课程管理",
						"线下班级"
					]
					for (let s of combinedArray) {
						if (list.includes(s.meta.title)) {
							asdfasdfasd.push(s)
						}
					}

				} else {

					let aaa = JSON.parse(res.data)
					for (let s of combinedArray) {
						if (aaa.includes(s.id)) {
							asdfasdfasd.push(s)
						}
					}

				}

				resolve(asdfasdfasd)
			})
		})

	}
}

export default constant